#include<iostream>
#include<set>
using namespace std;
set<string>t;
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string x;
		cin >> x;
		t.insert(x);
	}
	int m;
	int count = 0;
	string a;
	string b;
	cin >> m;
	while (m--) {
		string x;
		cin >> x;
		if (t.count(x)) {
			count++;
			if (a.empty() || a.substr(6, 8) > x.substr(6, 8)) {
				a = x;
			}
		}
		if (b.empty() || x.substr(6, 8) > b.substr(6, 8)) {
			b = x;
		}
	}
	cout << count << endl;
	if (count) {
		cout << a << endl;
	}
	else {
		cout << b << endl;
	}
	return 0;
}